package com.training2.bookstore.product.service;

import java.util.Collection;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import com.training2.bookstore.product.mapper.ProductMapper;
import com.training2.bookstore.product.model.Product;
import com.training2.bookstore.product.repo.ProductRepository;

@Service

public class ProductServiceSdata implements ProductService {
//	@Autowired ProductRepository rep;
	@Autowired ProductMapper mapper;
	@Override
	public Collection<Product> listProduct() {
		// TODO Auto-generated method stub
		return mapper.selectAllProducts();
	}

	public Product create1(Product p) {
		String id = UUID.randomUUID().toString();
		p.setId(id);
		mapper.saveProduct(p);
		return p;
	}

	@Override
	public Product create(Product p) {
		String id = UUID.randomUUID().toString();
		p.setId(id);
		mapper.saveProduct(p);
		return p;
	}

	@Override
	public Product update(Product p) {
		//rep.save(p);
		return p;
	}

	@Override
	public Product remove(String id) {
		Product p = mapper.selectProducById(id);
		 mapper.deleteById(id);
		 return p;
	}

	@Override
	public void storeProductImage(String productId, String storedFileId) {
		Product p = mapper.selectProducById(productId);
		p.setImageLocation(storedFileId);
		mapper.saveProduct(p);
		
		
	}

	@Override
	public Collection<Product> search(String key) {
		
		return mapper.findByNameLikeOrDescLike(key, key);
	}

}
